package com.ntd.study.base.proxy;

import cn.hutool.core.lang.Console;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxy implements InvocationHandler { // jdk 动态代理

    private Object target;

    public DynamicProxy(Object target) {
        super();
        this.target = target;
    }

    @SuppressWarnings("unchecked")
    public <T> T getProxy() {
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
        Console.log("代理 - 方法执行之前。。。");
        final Object res = method.invoke(target, objects);
        Console.log("代理 - 方法执行之后。。。");
        return res;
    }
}
